package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;

import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {


    private static final Logger log = LoggerFactory.getLogger(ShoppingCartServiceImpl.class);
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    //查询购物车中商品
    @Override
    public List<ShoppingCart> cartList() {
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> list =  shoppingCartMapper.cartList(shoppingCart);
      return list;
    }

    /**
     * 添加购物车
     * @param shoppingCartDTO
     */
    @Override
    public void addCart(ShoppingCartDTO shoppingCartDTO) {
        //判断当前商品购物车是否存在
         ShoppingCart shoppingCart = new ShoppingCart();
         shoppingCart = shoppingCartMapper.cartByDishIdOrSetmealId(shoppingCartDTO.getDishId(),shoppingCartDTO.getSetmealId());

        //如果已经存在了，只需要将数量加一
        if (shoppingCart != null){
            shoppingCart.setNumber(shoppingCart.getNumber() + 1);
            shoppingCartMapper.update(shoppingCart);
        }else {
            //获取用户id
            Long userId = BaseContext.getCurrentId();
            System.out.println("用户id："+userId);
            //如果不存在，需要插入一条购物车数据
            //判断本次添加到购物车的是菜品还是套餐
            if (shoppingCartDTO.getDishId() != null){
                //获取金额、菜名、图片
                BigDecimal amountDish = shoppingCartMapper.getDishAmount(shoppingCartDTO.getDishId());
                String imageDish = shoppingCartMapper.getDishImage(shoppingCartDTO.getDishId());
                String nameDish = shoppingCartMapper.getNameDish(shoppingCartDTO.getDishId());
                // 确保金额不为null
                if (amountDish == null) {
                    amountDish = BigDecimal.ZERO;
                }

                ShoppingCart shoppingDishCart = ShoppingCart.builder()
                        .dishFlavor(shoppingCartDTO.getDishFlavor())
                        .userId(userId)
                        .dishId(shoppingCartDTO.getDishId())
                        .name(nameDish)
                        .amount(amountDish)
                        .image(imageDish)
                        .number(1)
                        .createTime(LocalDateTime.now())
                        .build();
                shoppingCartMapper.addCart(shoppingDishCart);
            }else if (shoppingCartDTO.getSetmealId() != null){
                //获取金额amount
                BigDecimal amountSetmeal = shoppingCartMapper.getSetmealAmount(shoppingCartDTO.getSetmealId());
                //获取图片
                String imageSetmeal = shoppingCartMapper.getSetmealImage(shoppingCartDTO.getSetmealId());
                //获取套餐或者菜品名称
                String nameSetmeal = shoppingCartMapper.getNameSetmeal(shoppingCartDTO.getSetmealId());
                //添加套餐
                if (shoppingCartDTO.getSetmealId() != null){
                    // 确保金额不为null
                    if (amountSetmeal == null) {
                        amountSetmeal = BigDecimal.ZERO;
                    }
                    ShoppingCart shoppingSetmealCart = ShoppingCart.builder()
                            .dishFlavor(shoppingCartDTO.getDishFlavor())
                            .userId(userId)
                            .setmealId(shoppingCartDTO.getSetmealId())
                            .amount(amountSetmeal)
                            .image(imageSetmeal)
                            .name(nameSetmeal)
                            .number(1)
                            .createTime(LocalDateTime.now())
                            .build();
                    shoppingCartMapper.addCart(shoppingSetmealCart);
                }
            }

        }

    }

    //删除购物车
    @Override
    public void delete(ShoppingCartDTO shoppingCartDTO) {
        //判断删除的商品数量是否大于1
        //判断当前商品购物车是否存在
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart = shoppingCartMapper.cartByDishIdOrSetmealId(shoppingCartDTO.getDishId(),shoppingCartDTO.getSetmealId());
       shoppingCart.getNumber();
        if (shoppingCart != null){
            //如果已经存在了，只需要将数量加一
            if (shoppingCart.getNumber()>1){
                shoppingCart.setNumber(shoppingCart.getNumber() -1);
                shoppingCartMapper.update(shoppingCart);
            }else if (shoppingCart.getNumber() == 1){
                shoppingCartMapper.delete(shoppingCartDTO);
            }
        }
        }


    //清空购物车
    @Override
    public void clean() {
        Long userId = BaseContext.getCurrentId();
        shoppingCartMapper.clean(userId);
    }
}
